function changedOccured() {
	var cancel = document.getElementById('notes_cancel');
	var save = document.getElementById('notes_save');
	cancel.disabled = false;
	save.disabled = false;
}

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support AJAX");
	}
}

//Our XmlHttpRequest object to submit form
var saveReq = getXmlHttpRequestObject();

function handleSaveAck(){
	if (saveReq.readyState == 4) {
		var content = document.getElementById('content');
		//enable textarea
		content.disabled = false;
		if (saveReq.responseText == 'ok'){
			var original = document.getElementById('notes_content_original');
			//update original data
			original.innerHTML = content.value;
		}else{
			//error
			var errorMsg = document.getElementById('notes_errorMessage');
			errorMsg.className = 'notes_errorMessage_show';
			var cancel = document.getElementById('notes_cancel');
			var save = document.getElementById('notes_save');
			cancel.disabled = false;
			save.disabled = false;
		}
	}
}

function save(){
	var errorMsg = document.getElementById('notes_errorMessage');
	errorMsg.className = 'notes_errorMessage';
	var content = document.getElementById('content');
	var cancel = document.getElementById('notes_cancel');
	var save = document.getElementById('notes_save');
	cancel.disabled = true;
	save.disabled = true;
	content.disabled = true;
	//submit form, using POST
	saveReq.open("POST", '/notes/', true);
	saveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	saveReq.onreadystatechange = handleSaveAck;
	params = "content="+encodeURIComponent(content.value);
	saveReq.send(params);
}

function cancel(){
	var cancel = document.getElementById('notes_cancel');
	var save = document.getElementById('notes_save');
	cancel.disabled = true;
	save.disabled = true;
	//reload original data
	var content = document.getElementById('content');
	var original = document.getElementById('notes_content_original');
	content.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
}
